//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package board_dao

import (
	"context"

	"gitcode.com/eteam/api-server/dao/common"
	"gitcode.com/eteam/proto-gen-go.git/board_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type BasicLlmInfoDbItem struct {
	Name             string `bson:"name"`
	ApiKey           string `bson:"apiKey"`
	ApiAddr          string `bson:"apiAddr"`
	SystemPrompt     string `bson:"systemPrompt"`
	DefaultModelName string `bson:"defaultModelName"`
}

type LlmInfoDbItem struct {
	LlmId      string             `bson:"_id"`
	BoardId    string             `bson:"boardId"`
	BasicInfo  BasicLlmInfoDbItem `bson:"basicInfo"`
	CreateTime int64              `bson:"createTime"`
	UpdateTime int64              `bson:"updateTime"`
}

func (item *LlmInfoDbItem) ToLlmInfo() *board_api.LlmInfo {
	return &board_api.LlmInfo{
		LlmId: item.LlmId,
		BasicInfo: &board_api.BasicLlmInfo{
			Name:             item.BasicInfo.Name,
			ApiKey:           item.BasicInfo.ApiKey,
			ApiAddr:          item.BasicInfo.ApiAddr,
			SystemPrompt:     item.BasicInfo.SystemPrompt,
			DefaultModelName: item.BasicInfo.DefaultModelName,
		},
		CreateTime: item.CreateTime,
		UpdateTime: item.UpdateTime,
	}
}

type _BoardLlmDao struct {
	mc *mongo.Collection
}

func newBoardLlmDao(db *mongo.Database) (*_BoardLlmDao, error) {
	mc := db.Collection(common.DB_NS_BOARD + "llm")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"boardId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_BoardLlmDao{mc: mc}, nil
}

func (db *_BoardLlmDao) Insert(ctx context.Context, dbItem *LlmInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_BoardLlmDao) Exist(ctx context.Context, boardId, llmId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"boardId": boardId, "_id": llmId})
	return count > 0, err
}

func (db *_BoardLlmDao) Get(ctx context.Context, boardId, llmId string) (*LlmInfoDbItem, error) {
	retItem := &LlmInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"boardId": boardId, "_id": llmId}).Decode(retItem)
	return retItem, err
}

func (db *_BoardLlmDao) Remove(ctx context.Context, boardId, llmId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"boardId": boardId, "_id": llmId})
	return err
}

func (db *_BoardLlmDao) RemoveByBoard(ctx context.Context, boardId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"boardId": boardId})
	return err
}

func (db *_BoardLlmDao) UpdateBasic(ctx context.Context, boardId, llmId string, basicItem *BasicLlmInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"boardId": boardId, "_id": llmId},
		bson.M{"$set": bson.M{
			"basicInfo":  basicItem,
			"updateTime": updateTime,
		}})
	return err
}

func (db *_BoardLlmDao) ListByBoard(ctx context.Context, boardIdList []string) ([]*LlmInfoDbItem, error) {
	if len(boardIdList) == 0 {
		return []*LlmInfoDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"boardId": bson.M{"$in": boardIdList}})
	if err != nil {
		return nil, err
	}
	retList := []*LlmInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}
